<?php

declare(strict_types=1);

namespace App\CustHub\Mapper;

use App\CustHub\Model\ChTalkUserExtendInfo;
use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Config\Annotation\Value;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;
use Mine\Abstracts\AbstractMapper;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ChTalkUserExtendInfoMapper extends AbstractMapper
{

    #[Value('cache.default.prefix')]
    protected string $prefix;

    public function assignModel()
    {
        $this->model = ChTalkUserExtendInfo::class;
    }

    /**
     * 获取用户扩展信息
     * @param int $csId
     * @param string $userId
     * @return \Mine\MineModel|Model|string|Builder|null
     */
    #[Cacheable(prefix: 'talk_user_extend_info', value: '#{csId}:#{userId}', ttl: 15 * 60)]
    public function getInfo(int $csId,string $userId): \Mine\MineModel|Model|string|Builder|null
    {
        return $this->model::query()->where('cs_id',$csId)->where('user_id',$userId)->first();
    }

    /**
     * 删除用户缓存
     * @param mixed $id
     * @return void
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \RedisException
     */
    public function clearCache(int $csId,string $userId): void
    {
        $redis = redis();
        $keyOne = $this->prefix . 'talk_user_extend_info:'.$csId.':'.$userId;
        $redis->del([$keyOne]);
    }
}